package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.google.gson.Gson;

import ds.IClientDS;
import ds.IListDS;
import entities.Item;
import entities.ItemList;
import entities.User;

/**
 * Servlet implementation class ListManager
 */
@WebServlet("/ListManager")
public class ListManager extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ListManager() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Get the loaded context
		System.out.println("GET");
		WebApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		System.out.println(request.getParameter("action"));
		// Instanciate the domain service ClientDS
		IClientDS clientDS = (IClientDS) ctx.getBean("clientDS");
		IListDS listeDS = (IListDS) ctx.getBean("listDS");

		String login = ((User) request.getSession().getAttribute("user"))
				.getLogin();
		User userInDB = clientDS.find(login);

		if ("checkItem".equals(request.getParameter("action"))) {
			listeDS.checkItem(Integer.parseInt(request.getParameter("idItem")));
			System.out.println("check idItem : "
					+ Integer.parseInt(request.getParameter("idItem")));
		}

		if ("addItem".equals(request.getParameter("action"))) {
			System.out.println(("add item : " + request.getParameter("title")));
			for (ItemList list : userInDB.getAllLists()) {
				if (list.getId() == Integer.parseInt(request
						.getParameter("listId"))) {
					Item item = new Item();
					item.setTitle(request.getParameter("title"));
					list.getItems().add(item);
					listeDS.addItem(list, item);
				}
			}
		} else if ("removeItem".equals(request.getParameter("action"))) {
			for (ItemList list : userInDB.getAllLists()) {
				if (list.getId() == Integer.parseInt(request
						.getParameter("listId"))) {
					listeDS.removeItem(list,
							Integer.parseInt(request.getParameter("itemId")));
				}
			}
		} else if ("removeList".equals(request.getParameter("action"))) {
			System.out.println("remove list : "
					+ Integer.parseInt(request.getParameter("listId")));
			if (request.getParameter("listId") != null) {
				ItemList listToRemove = null;
				for (ItemList list : userInDB.getAllLists()) {
					if (list.getId() == Integer.parseInt(request
							.getParameter("listId"))) {
						listToRemove = list;
					}
				}
				listeDS.removeList(userInDB, listToRemove);
			}
		} else if ("createList".equals(request.getParameter("action"))) {
			ItemList l = new ItemList();
			l.setName(request.getParameter("name"));
			l.setCategory(request.getParameter("category"));
			l.setCreationDate(new Date());
			l.setItems(new ArrayList<Item>());

			listeDS.addList(userInDB, l);
		} else if ("displayAll".equals(request.getParameter("action"))) {
			response.setContentType("application/json");
			System.out.println("userInDB : " + userInDB.getAllLists().size());
			response.getWriter().write(
					new Gson().toJson(userInDB.getAllLists()));
		}

		else if ("displayContent".equals(request.getParameter("action"))) {
			System.out.println("display list : "
					+ request.getParameter("listId"));
			if (request.getParameter("listId") != null) {
				for (ItemList list : userInDB.getAllLists()) {
					if (list.getId() == Integer.parseInt(request
							.getParameter("listId"))) {
						response.setContentType("application/json");
						response.getWriter().write(new Gson().toJson(list));
					}
				}
			}
		}

		else {
			request.getServletContext().getRequestDispatcher("/jsp/home.jsp")
					.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}
